<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

switch($task){
	
	case 'quickadd':
		
		$sql = "SELECT ordering FROM #__leagueman_season ORDER BY ordering DESC LIMIT 1";
		$database->setQuery($sql);
		$ordering = $database->loadResult() + 1;
		
		$season_name = mosGetParam($_POST, "season_name", "");
		if(!empty($season_name)){
			$row = LM::getLMTable("Season");
			$row->season_name = $season_name;
			$row->ordering = $ordering;
			$row->store();
		}
		
		LM::redirect("seasons");
		
	break;
	
	case 'new':
	case 'edit':
	
		$row = LM::getLMTable("Season");
		$row->load( $cid[0] );
		
		$order	= mosGetOrderingList( "SELECT ordering AS value, season_name AS text FROM #__leagueman_season ORDER BY ordering");
		$list	= mosHTML::selectList( $order, 'ordering', 'class="inputbox" size="1"','value', 'text', intval( $row->ordering ) );
		
		$template = LM::getTemplate();
		
		$template->assign("view", 		"admin/form_seasons.tpl.php");
		$template->assign("project",	$project);
		$template->assign("team",		$team);
		$template->assign("my",			$my);
		$template->assign("row",		$row);
		$template->assign("list",		$list);
		$template->display("admin/view.tpl.php");
	break;
	
	case 'save':
		$row = LM::getLMTable("Season");
		$row->save($_POST);
		LM::redirect("seasons");
	break;
	
	case 'cancel':
		LM::redirect("seasons");
	break;
	
	case 'orderup':
		$row = LM::getLMTable("Season");
		$row->load( $cid[0] );
		$row->move( -1, "" );
		LM::redirect("seasons");
	break;
	
	case 'orderdown':
		$row = LM::getLMTable("Season");
		$row->load( $cid[0] );
		$row->move( 1, "" );
		LM::redirect("seasons");
	break;
	
	case 'remove':
		$row = LM::getLMTable("Season");
		foreach($cid AS $c){
			$row->delete($c);
		}
		LM::redirect("seasons");
	break;
	
	default:
		
		$limit		= LM::getSessionValueFromRequest( "_{$act}_limit", 'limit', 10 );
		$limitstart = LM::getSessionValueFromRequest( "_{$act}_limitstart", 'limitstart', 0 );
		
		$database->setQuery( "SELECT COUNT(*) FROM #__leagueman_season AS a");
        $total = $database->loadResult();
        $pageNav = LM::getPageNav( $total, $limitstart, $limit, 1);
        
        $database->setQuery( "SELECT a.*
                                FROM #__leagueman_season AS a
                            ORDER BY a.ordering
                               LIMIT $pageNav->limitstart,$pageNav->limit");
        $rows = $database->loadObjectList();
		
		$template = LM::getTemplate();
		
		$template->assign("view", "admin/list_seasons.tpl.php");
		$template->assign("project", $project);
		$template->assign("team", $team);
		$template->assign("rows", $rows);
		$template->assign("pageNav", $pageNav);
		$template->assign("my",$my);
		$template->display("admin/view.tpl.php");
	break;
}

?>